package cn.bocweb.spinning.view.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.GetMsgBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.SystemMsgMainAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.SwipeRefreshHelper;
import de.greenrobot.event.EventBus;

public class SystemMsgActivity extends BaseActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.listView)
    ListView listView;

    List<GetMsgBean.DataBean> list;
    SystemMsgMainAdapter adapter;
    UserPresenter presenter;
    String userid;
    int page = 1;
    SwipeRefreshHelper swipeRefreshHelper;
    @Bind(R.id.system_refresh)
    SwipeRefreshLayout systemRefresh;
    boolean isLastPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_msg);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        loadData();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void loadData() {

        UserBean.DataEntity user = new UserBean.DataEntity();
        user.readInfoFromLocal(this);
        userid = user.getUserid();

        presenter = new UserPresenterImp();

        presenter.getMsg(userid, "1", "9", String.valueOf(page));

        list = new ArrayList<>();
        adapter = new SystemMsgMainAdapter(this, list);

        toolbar.setTitle("系统消息");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        listView.setAdapter(adapter);

        swipeRefreshHelper = new SwipeRefreshHelper(systemRefresh, listView, new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                list.clear();
                isLastPage = false;
                adapter.notifyDataSetChanged();
                presenter.getMsg(userid, "1", "9", String.valueOf(page));
            }

            @Override
            public void onLoad() {
                if (!isLastPage) {
                    page ++;
                    presenter.getMsg(userid, "1", "9", String.valueOf(page));
                } else {
                    DeviceUtil.MyToast(SystemMsgActivity.this, "已经是最后一页");
                }
            }
        });
    }

    public void onEventMainThread(GetMsgBean bean) {
        if (bean.getReturn_code() == 0) {
            list.addAll(bean.getData());
            adapter.notifyDataSetChanged();
            if (systemRefresh.isRefreshing()) {
                systemRefresh.setRefreshing(false);
            }
        } else if (bean.getReturn_code() == -203) {
            DeviceUtil.MyToast(SystemMsgActivity.this, "已经是最后一页");
            isLastPage = true;
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
    }
}
